<template>
  <div class="app-container">
    <el-row :gutter="10"  class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="substituteList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column type="index" label="序号" align="center" width="50" />
          <el-table-column label="物料编码" width = "120" align="center" key="itemCode" prop="itemCode"  >
              <!-- <protemplate slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="handleView(scope.row)"
                  v-hasPermi="['mes:md:mditem:query']"
                >{{scope.row.itemCode}}</el-button>
              </protemplate> -->
          </el-table-column>
          <el-table-column label="物料名称"  align="center" key="itemName" prop="itemName"  :show-overflow-tooltip="true" />
          <el-table-column label="规格型号" width="210" align="center" key="specification" prop="specification" :show-overflow-tooltip="true" />
          <el-table-column label="单位" align="center"  prop="measureName"  :show-overflow-tooltip="true" >
          </el-table-column>
          <!-- <el-table-column label="物料/产品" align="center" key="itemOrProduct" prop="itemOrProduct" v-if="columns[4].visible" :show-overflow-tooltip="true" >
            <protemplate slot-scope="scope">
              <dict-tag :options="dict.type.mes_item_product" :value="scope.row.itemOrProduct"/>
            </protemplate>
          </el-table-column> -->

          <el-table-column label="所属分类" align="center" key="categoryName" prop="categoryName" v-if="columns[5].visible" width="120" />
          <el-table-column label="是否启用" align="center" key="enableFlag" >
            <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
            </template>
          </el-table-column>
          <!-- <el-table-column label="设置安全库存" align="center" key="safeStockFlag" v-if="columns[7].visible">
            <protemplate slot-scope="scope">
              <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.safeStockFlag"/>
            </protemplate>
          </el-table-column> -->
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[8].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">

              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />


    <!-- 添加或修改替代品模板-产品对话框 -->

      <ItemSelect ref="itemSelect" @onSelected="onItemSelected" ></ItemSelect>

  </div>
</template>

<script>
import { listSubstitute, getSubstitute, delSubstitute, addSubstitute, updateSubstitute } from "@/api/mes/md/substitute";
import ItemSelect  from "@/components/itemSelect/single.vue";
export default {
  name: "substitute",
  dicts: ['sys_yes_no','mes_item_product'],
  components: {ItemSelect},
  props:{
    sourceJdyItemId: null
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测模板-产品表格数据
      substituteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      columns: [
        { key: 0, label: `物料/产品编码`, visible: true },
        { key: 1, label: `物料/产品名称`, visible: true },
        { key: 2, label: `规格型号`, visible: true },
        { key: 3, label: `单位`, visible: true },
        { key: 4, label: `物料/产品`, visible: true },
        { key: 5, label: `物料分类`, visible: true },
        { key: 6, label: `是否启用`, visible: true },
        { key: 7, label: `是否设置安全库存`, visible: true },
        { key: 8, label: `创建时间`, visible: true }
      ],
      // 查询参数
      queryParams: {
        sourceJdyItemId:this.sourceJdyItemId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateId: [
          { required: true, message: "检测模板ID不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "产品物料不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检测模板-产品列表 */
    getList() {
      this.loading = true;
      listSubstitute(this.queryParams).then(response => {
        this.substituteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sourceJdyItemId:this.sourceJdyItemId,
        targetJdyItemId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加替代产品";
      this.$refs.itemSelect.showFlag = true;
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getsubstitute(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改检测模板-产品";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.id != null) {
          //   updatesubstitute(this.form).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
            addSubstitute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除替代产品？').then(function() {
        return delSubstitute(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    //物料选择弹出框
    onItemSelected(obj){
        if(obj != undefined && obj != null){
          this.form.targetJdyItemId = obj.jdyItemId;
          addSubstitute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
    },
  }
};
</script>
